from django.http.request import QueryDict
from django.utils.safestring import mark_safe


class Paging(object):
    def __init__(self, page, num, params=None, page_num=20, max_show=5):
        """

        :param page: 第几页
        :param num: 数据量
        :param params:
        :param page_num: 每页展示多少数据
        :param max_show:
        """
        self.params = params
        self.max_show = max_show
        if not self.params:
            self.params = QueryDict(mutable=True)
        try:
            self.page = int(page)
        except Exception:
            self.page = 1
        if num <= 0:
            num = 1

        if self.page <= 0:
            self.page = 1

        self.page_total, mode = divmod(num, page_num)

        # 最大显示页码
        if mode:
            self.page_total += 1

        if self.page > self.page_total:
            self.page = self.page_total

        # 计算页码前后数量
        self.page_show = self.max_show // 2

        # 计算显示的页码首尾
        if self.page_total <= self.max_show:
            self.page_start = 1
            self.page_end = self.page_total
        else:
            if self.page - self.page_show <= 0:
                self.page_start = 1
                self.page_end = self.max_show
            elif self.page + self.page_show > self.page_total:
                self.page_start = self.page_total - self.max_show + 1
                self.page_end = self.page_total
            else:
                self.page_start = self.page - self.page_show
                self.page_end = self.page + self.page_show

        # 显示数据的开始和结束
        self.start = (self.page - 1) * page_num
        self.end = self.page * page_num

    @property
    def page_html(self):
        page_list = []

        # 上一页
        if self.page == 1:
            page_list.append('<a class="disabled">上一页</a>')
        else:
            self.params['page'] = self.page - 1
            page_list.append('<a href="?{}">上一页</a>'.format(self.params.urlencode()))
            if self.page > self.max_show - self.page_show and self.page_total > self.max_show:
                self.params['page'] = 1
                page_list.append('<a href="?{}">1</a>'.format(self.params.urlencode()))
                page_list.append('<span>...</span>')

        # 页码
        for i in range(self.page_start, self.page_end + 1):
            self.params['page'] = i
            if i == self.page:
                page_list.append('<a class="active">{}</a>'.format(i))
            else:
                page_list.append('<a href="?{}">{}</a>'.format(self.params.urlencode(), i))

        # 下一页
        if self.page == self.page_total:
            page_list.append('<a class="disabled">下一页</a>')
        else:
            if self.page < self.page_total - self.page_show and self.page_total > self.max_show:
                self.params['page'] = self.page_total
                page_list.append('<span>...</span>')
                page_list.append('<a href="?{}">{}</a>'.format(self.params.urlencode(), self.page_total))
            self.params['page'] = self.page + 1
            page_list.append('<a href="?{}">下一页</a>'.format(self.params.urlencode()))

        return mark_safe(''.join(page_list))
